package cn.interestingshop.servlet;

import cn.interestingshop.entity.User;
import cn.interestingshop.service.UserService;
import cn.interestingshop.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/UserInsertServlet")
public class UserInsertServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取请求参数
        String account = request.getParameter("loginName");
        String nickName = request.getParameter("userName");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String idCardNo = request.getParameter("identityCode");
        String email = request.getParameter("email");
        String phone = request.getParameter("mobile");
        String type = request.getParameter("type");
        //3.调用service进行业务处理
        UserService service=new UserServiceImpl();
        User user=new User();
        user.setAccount(account);
        user.setNickName(nickName);
        user.setPassword(password);
        user.setGender(Integer.parseInt(gender));
        user.setIdCardNo(idCardNo);
        user.setEmail(email);
        user.setPhone(phone);
        user.setType(Integer.parseInt(type));

        int i = service.insert(user);
        //4.分析返回结果
        if(i>0){
            response.getWriter().print("<script>alert('新增成功');location.href='../back/UserSelectAllServlet'</script>");
        }else{
            response.getWriter().print("<script>alert('新增失败');history.go(-1)</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
